package com.tencent.miniproject.presenter;

import android.util.Log;

import com.tencent.miniproject.been.MainBeen;
import com.tencent.miniproject.constants.Constant;
import com.tencent.miniproject.fragment.i.INeighborFriendsFragment;
import com.tencent.miniproject.presenter.i.INeighborFriendsPresenter;
import com.tencent.miniproject.utils.GsonUtil;
import com.tencent.miniproject.utils.OkHttpResultCallback;
import com.tencent.miniproject.utils.OkHttpUtil;

import okhttp3.Call;

/**
 * Created by PC on 2018/5/30.
 */

public class NeighborFriendsPresenter implements INeighborFriendsPresenter{
    private static final String TAG = "NeighborFriendsPresente";
    private INeighborFriendsFragment mINeighborFriendsFragment;

    public NeighborFriendsPresenter(INeighborFriendsFragment iNeighborFriendsFragment) {
        mINeighborFriendsFragment = iNeighborFriendsFragment;
    }

    @Override
    public void getPostData(int type,Double longitude, Double latitude, int pageNo,String tag) {
        Log.e(TAG, "getPostData: type: "+ type );
        String url = "";
        if (type == Constant.FRAGMENT_NEIGHBOR){
             url = Constant.BASEURL+ "post/getPostsAround?longitude="+longitude+"&latitude="
                    +latitude+"&pageId="+pageNo;
        }else if (type == Constant.FRAGMENT_FRIENDS){
            url = Constant.BASEURL+ "post/getFriendPosts?longitude="+longitude+"&latitude="
                    +latitude+"&pageId="+pageNo;
        }else if (type == Constant.FRAGMENT_TAG){
            url = Constant.BASEURL+"search/getPosts?tag="+tag;
        }
        Log.e(TAG, "getPostData: " + url);
        OkHttpUtil.getInstance().getAsync(url, new OkHttpResultCallback() {
            @Override
            public void onError(Call call, Exception e) {
                mINeighborFriendsFragment.getPostDataError(e.getMessage());
            }

            @Override
            public void onResponse(byte[] bytes) {
                try {
                    String msg = new String(bytes,"utf-8");
                    Log.e(TAG, "onResponse: " + msg);
                    MainBeen mainBeen = GsonUtil.gsonToBean(msg,MainBeen.class);
                    if (mainBeen.getResult().equals("success")){
                        mINeighborFriendsFragment.getPostDataSuccess(mainBeen.getMessage());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
